Add fluent Assertion builder for type-safe assertion definitions#38
Closed
Add fluent Assertion builder for type-safe assertion definitions#38
Conversation
Implement a fluent API for building Cronitor assertion strings with type safety and discoverability, replacing raw string construction. https://claude.ai/code/session_01XFQ49AHWaNZH3pyffR78LN
The Assertions property now accepts AssertionRule objects instead of raw strings. AssertionRule serializes to/from JSON as a plain string via a custom JsonConverter, so the API payload is unchanged. Implicit conversions between string and AssertionRule are supported for backwards compatibility. https://claude.ai/code/session_01XFQ49AHWaNZH3pyffR78LN
c725347 to
772c627
Compare
The AssertionContainer test class lacked a [JsonPropertyName] attribute, so System.Text.Json could not match the lowercase JSON key to the PascalCase property, leaving it null. https://claude.ai/code/session_01XFQ49AHWaNZH3pyffR78LN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements a fluent builder API for Cronitor assertions, providing type safety and discoverability when defining monitor assertions instead of using raw strings.
Key Changes
New
Assertionclass inCronitor.Constantswith fluent builder patternAssertion.Metricprovides builders forDuration,Count, andErrorCountAssertion.Responseprovides builders forCode,Time,Body, and parameterizedJson()andHeader()methodsEquals(),LessThan(),GreaterThan(), andContains()New
AssertionRuleclass to represent assertion expressionsstringfor backward compatibilityAssertionRuleConverter) for proper serialization/deserialization[JsonConverter]attribute for automatic handlingUpdated
Monitor.Assertionsproperty to useIEnumerable<AssertionRule>instead ofIEnumerable<string>Updated tests and builders to use the new fluent API
AssertionTestsclass with 11 test cases covering all assertion types and serializationMonitorTypeTests,JobBuilder, and request tests to useAssertionRuletypeUpdated README with usage examples and marked the assertion language feature as complete
Notable Implementation Details
AssertionBuilderclass uses a private constructor pattern to enforce fluent API usageAssertionRuleobjects as simple strings for API compatibilitymetric.duration < 30s) and parameterized ones (e.g.,response.json user.count > 10)